<template>
  <view class="content">
    <view class="avatar-uploader">
      <image class="avatar" :src="avatarUrl" @click="chooseImage"></image>
      <button @click="chooseImage">选择图片</button>
      <button @click="uploadImage" :disabled="!imagePath">上传头像</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imagePath: '/static', // 本地路径
      avatarUrl: '/static/background1.jpg' ,// 默认图片
	   Token:{
	  		  "token":'',
	  	  },
	  	  NewUserInfo:{
	  		
	  	  }
    };
  },
  onLoad:function(){
    	this.Token.token=uni.getStorageSync('login-user').token;
		this.NewUserInfo=uni.getStorageSync('UserInfo');

			},
  methods: {
    chooseImage() {
      const that = this;
      uni.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表 tempFilePaths
          that.imagePath = res.tempFilePaths[0];
          that.avatarUrl = res.tempFilePaths[0]; // 显示选中的图片
        }
      });
    },
    uploadImage() {
      const that = this;
      const uploadTask = uni.uploadFile({
        url: 'http://192.168.2.156:8086/business/common2/upload2', // 仅为示例，非真实的接口地址
        filePath: that.imagePath,
        name: 'file',
        header:{
        	"token":this.Token.token
        		  },
        success: (res) => {
        console.log(JSON.parse(res.data).data);
		this.NewUserInfo.photo=JSON.parse(res.data).data;
		

		this.changeInfo();
          // 上传完成操作
        }
      });

      uploadTask.onProgressUpdate((res) => {

        // 可以在这里处理上传进度条等效果
      });
    },
	
	changeInfo(){
		let new_url;
		if(uni.getStorageSync('login-role')==1)
		new_url='http://192.168.2.156:8086/business/user/update';
		else
		new_url='http://192.168.2.156:8086/business/seller/update';
	
		uni.request({
			method:'POST',
			url:new_url,
			header:{
				"token":this.Token.token
			},
			data:this.NewUserInfo,
			success() {
				console.log("成功");
			}
			
		})
	}
	
  }
};
</script>

<style>
.content {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.avatar-uploader {
  margin-bottom: 20px;
}
.avatar {
  width: 200px;
  height: 200px;
  background-color: #ddd;
  margin-bottom: 10px;
}
</style>